package me.youline.dataServer.service;

import javax.transaction.Transactional;

import me.youline.dataServer.dao.MsgPraiseRecordDAO;
import me.youline.dataServer.dao.PubMsgDAO;
import me.youline.dataServer.dao.PubMsgStatisticRecorderDAO;
import me.youline.dataServer.entity.PublicMessage;
import me.youline.dataServer.exception.ReplicatedDataException;
import me.youline.dataServer.exception.UnExistPubMsgException;
import me.youline.dataServer.exception.UnPermitAccessException;
import me.youline.dataServer.service.front.UserInfoDTOAdapter;
import me.youline.dataServer.service.socialAction.SocialActionEventPublisher;
import me.youline.dataServer.service.socialAction.event.PraiseSocialActionCancelEvent;
import me.youline.dataServer.service.socialAction.event.PubMsgSocialActionExeEvent.PraiseSocialActionExeEvent;
import me.youline.dataServer.service.socialAction.event.PubMsgSocialActionExeEvent.PubMsgBaseInfo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 点赞消息记录业务类
 * 
 * @author linhan
 */
@Service
public class MsgPraiseRecordService {

	@Autowired
	private MsgPraiseRecordDAO recordDAO;

	@Autowired
	private UserInfoDTOAdapter assembler;

	@Autowired
	private PubMsgStatisticRecorderDAO msgRecorderDAO;

	@Autowired
	private PubMsgDAO pubMsgDao;

	@Autowired
	private SocialActionEventPublisher publisher;

	/**
	 * 为指定消息点赞
	 * 
	 * @param msgId
	 * @param userId
	 * @return 返回当前点赞数
	 * @throws UnPermitAccessException
	 * @throws ReplicatedDataException
	 * @throws UnExistPubMsgException
	 */
	@Transactional
	public int doMessagePraising(Long msgId, Long userId)
			throws UnPermitAccessException, ReplicatedDataException,
			UnExistPubMsgException {
		if (recordDAO.isExistPraiseRecord(msgId, userId)) {
			throw new ReplicatedDataException("用户已经为该消息点赞");
		}
		if(! pubMsgDao.exists(msgId)){
			throw new UnExistPubMsgException("公告不存在");
		}
		recordDAO.addMsgPraiseRecord(msgId, userId);
		msgRecorderDAO.addPraiseNum(msgId, 1);
		publishSocialActionEvent(msgId, userId);
		return msgRecorderDAO.findOne(msgId).getPraiseNum();
	}
	
	
	/**
	 * 发布点赞事件消息。
	 * @param currentMsgId 当前被点赞消息ID
	 * @param userId 发起的用户ID
	 */
	protected void publishSocialActionEvent(Long msgId, Long userId){
		PublicMessage msg = pubMsgDao.findOne(msgId);
		if(msg != null){
			publisher.publishSocialEvent(new PraiseSocialActionExeEvent(userId, new PubMsgBaseInfo(msg)));
		}
	}

	/**
	 * 删除点赞记录
	 * 
	 * @param msgId
	 * @param userId
	 */
	@Transactional
	public boolean deletePraiseRecord(Long msgId, Long userId) {
		if (recordDAO.deleteMsgPraiseRecord(msgId, userId)) {
			msgRecorderDAO.addPraiseNum(msgId, -1);
			publisher.publishSocialEvent(new PraiseSocialActionCancelEvent(userId, msgId));
			return true;
		}
		return false;
	}

	/**
	 * 判断用户是否能为指定原创消息点赞
	 * 
	 * @param originId
	 * @param userId
	 * @return
	 */
	public boolean isAbleToPraiseMsg(Long msgId, Long userId) {
		return !recordDAO.isExistPraiseRecord(msgId, userId);
	}

}
